package net.begincode.blog.service.impl;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import net.begincode.blog.bean.param.KeyWordsQueryParam;
import net.begincode.blog.bean.vo.HotKeyWord;
import net.begincode.blog.enums.KeyWordFlagEnum;
import net.begincode.blog.dao.biz.BlogKeyWordsBizMapper;
import net.begincode.blog.dao.biz.KeyWordsBizMapper;
import net.begincode.blog.entity.KeyWords;
import net.begincode.blog.service.KeyWordsService;
import net.begincode.page.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class KeyWordsServiceImpl implements KeyWordsService {
    private static final Logger LOGGER = LoggerFactory.getLogger(KeyWordsServiceImpl.class);
    @Resource
    private KeyWordsBizMapper keyWordsBizMapper;
    @Resource
    private BlogKeyWordsBizMapper blogKeyWordsBizMapper;

    @Override
    public int addKeyWord(KeyWords keyWords) {
        Preconditions.checkNotNull(keyWords, "keyword not null");
        return keyWordsBizMapper.insert(keyWords);
    }

    @Override
    public int updateKeyWordById(KeyWords keyWords) {
        Preconditions.checkNotNull(keyWords, "keyWords not null");
        Preconditions.checkArgument(keyWords.getId() != null, "keyWords id not null");
        return keyWordsBizMapper.updateByPrimaryKeySelective(keyWords);
    }

    @Override
    public int delKeyWordById(Long keyWordId) {
        Preconditions.checkNotNull(keyWordId, "keyWordId not null");
        int useKeyWordBlogCount = blogKeyWordsBizMapper.countByKeyWordIdAndFlag(keyWordId);
        if (useKeyWordBlogCount > 0) {
            LOGGER.warn("keywordId={},被其他博文使用，不能删除", keyWordId);
            return 0;
        }
        KeyWords keyWords = new KeyWords();
        keyWords.setId(keyWordId);
        keyWords.setFlag(KeyWordFlagEnum.UNEFFECT.getFlagCode());
        return keyWordsBizMapper.updateByPrimaryKeySelective(keyWords);
    }

    @Override
    public PageResponse<KeyWords> queryKeyWords(KeyWordsQueryParam keyWordsQueryParam) {
        int totalSize = keyWordsBizMapper.countKeyWords(keyWordsQueryParam.getKeyWordFlag(),
                keyWordsQueryParam.getKeyWord());
        if (totalSize == 0) {
            return PageResponse.empty(keyWordsQueryParam.getPageNo(), keyWordsQueryParam.getPageSize());
        }
        List<KeyWords> keyWords = keyWordsBizMapper.queryKeyWordsLimit(keyWordsQueryParam.getKeyWordFlag(),
                keyWordsQueryParam.getKeyWord(),
                keyWordsQueryParam.getOffset(),
                keyWordsQueryParam.getLimit());
        return PageResponse.success(keyWords, keyWordsQueryParam.getPageNo(), keyWordsQueryParam.getPageSize(), totalSize);
    }

    @Override
    public List<KeyWords> queryAllEffect() {
        return keyWordsBizMapper.queryAllByFlag(KeyWordFlagEnum.EFFECT.getFlagCode());
    }

    @Override
    public KeyWords selectById(Long keyWordId) {
        return keyWordsBizMapper.selectByPrimaryKey(keyWordId);
    }

    @Override
    public List<HotKeyWord> queryHotKeyWord(Integer limit) {
        List<HotKeyWord> hotKeyWords = keyWordsBizMapper.selectHotKeyWords(10);
        if(CollectionUtils.isEmpty(hotKeyWords)){
            return Lists.newArrayList();
        }
        List<Long> keyWordIds = net.begincode.common.CollectionUtils.safes(hotKeyWords)
                .stream().map(HotKeyWord::getKeyWordId).collect(Collectors.toList());
        Map<Long,KeyWords> keyWordsMap = keyWordsBizMapper.queryByIds(keyWordIds).stream()
                .collect(Collectors.toMap(KeyWords::getId, Function.identity()));
        hotKeyWords.stream().forEach(k->{
            if(keyWordsMap.containsKey(k.getKeyWordId())){
                k.setKeyWord(keyWordsMap.get(k.getKeyWordId()).getKeyWord());
            }else{
                LOGGER.error("没有对应的keywords");
            }
        });
        return hotKeyWords;
    }
}
